const { get, post } = require('../../utils/request');
Page({
  data: {
    carModelList: [
      {
        id: 30,
        carName: 'BJ30',
        image: '/images/BJ1.png'
      },
      {
        id: 32,
        carName: '全新BJ40',
        image: '/images/BJ2.png'
      },
      {
        id: 33,
        carName: 'BJ60雷霆',
        image: '/images/BJ3.png'
      },
      {
        id: 31,
        carName: 'BJ40增程',
        image: '/images/BJ4.png'
      },
      {
        id: 34,
        carName: '新X7',
        image: '/images/BJ5.png'
      }
    ]
  },
  onLoad: function(options) {
    // options就是传递过来的参数对象
    console.log(options.storeID); // 输出："123"（注意是字符串类型）
    // GET请求示例
    const _this = this
    get('/system/car/list', {        // 发送给服务器的数据（GET请求会自动拼接到URL）
      storeId: options.storeID
    }).then(res => {
      debugger

      console.log('接口返回数据:', res.data.rows);
      if (res.data.code === 200) {
        const data = _this.data.carModelList.filter(item1 => res.data.rows.find(item2=>item2.carId === item1.id+''))
        this.setData({
          carModelList: data // 将 rows 数组存入 storeList
        });
      }
    }).catch(err => {
      console.error(err);
    });
  },

  onBack() {
    wx.navigateBack();
  },

  onCarModelSelect(e) {
    const carModelId = e.currentTarget.dataset.id;
    const selectedCarModel = this.data.carModelList.find(carModel => carModel.id === carModelId);
    console.info("000000000000", selectedCarModel)
    // 这里可以将选择的车型信息返回给上一页
    wx.setStorageSync('selectedCarModel', selectedCarModel);
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; // 获取上一页实例
    if (prevPage) {
      prevPage.refreshData(); // 调用上一页的自定义刷新方法
    }
    wx.navigateBack();
  }
});